<!--
#-------------------------------------------------------------------------------
# Copyright 2011 Devesh Parekh
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#   http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#-------------------------------------------------------------------------------
-->
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.opentok.api.OpenTokSDK" %>
<%@ page import="com.google.appengine.api.users.UserService" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<%@ page import="com.google.appengine.api.users.UserServiceFactory" %>
<%@ page import="com.google.common.base.CharMatcher" %>
<%@ page import="com.google.common.base.Joiner" %>
<%@ page import="com.google.common.base.Splitter" %>
<%@ page import="javax.jdo.PersistenceManager" %>
<%@ page import="com.appspot.tokroom.Constants" %>
<%@ page import="com.appspot.tokroom.data.PMF" %>
<%@ page import="com.appspot.tokroom.data.Room" %>

<%
  String room = request.getParameter("room");
  if (room == null) {
    response.sendRedirect("/");
    return;
  }
  room = CharMatcher.JAVA_LETTER_OR_DIGIT.or(CharMatcher.WHITESPACE).retainFrom(room);

  UserService userService = UserServiceFactory.getUserService();
  PersistenceManager pm = PMF.get().getPersistenceManager();
  Room roomConfig;
  try {
    try {
      roomConfig = pm.getObjectById(Room.class, room);
    } catch (javax.jdo.JDOObjectNotFoundException e) {
      roomConfig = new Room(room, userService.getCurrentUser().getEmail());
    }
    if (!roomConfig.isAllowed(userService.getCurrentUser().getEmail())) {
      response.sendError(403, "You are not authorized to manage this room.");
      return;
    }
    if (request.getParameter("submit") != null) {
      // Save and redirect.
      roomConfig.setAllowedParticipants(Splitter.on("\n").trimResults().omitEmptyStrings().split(
        CharMatcher.JAVA_LETTER_OR_DIGIT.or(CharMatcher.anyOf(".@\n")).retainFrom(request.getParameter("participants"))));
      if (roomConfig.getSessionId() == null) {
        roomConfig.setSessionId(
           new OpenTokSDK(Constants.API_KEY, Constants.API_SECRET).create_session(request.getRemoteAddr())
               .getSessionId());
      }
      pm.makePersistent(roomConfig);
      response.sendRedirect("manage.jsp?room=" + room + "&saved=true");
      return;
    }
  } finally {
    pm.close();
  }
%>

<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>TokRoom</title>
<style type="text/css">
  label {display: block;}
  input {display:block;}
</style>
</head>

<body>
    <% if (request.getParameter("saved") != null) { %><div>Saved!</div><% } %>
    <form action="manage.jsp" method="post">
        <label for="room">Room name</label>
        <input type="text" name="room" value="<%= roomConfig.getName() %>" readonly/>
        <label for="participants">Allowed participants (one Google account per line). Empty for anybody.</label>
        <textarea name="participants" rows="10" cols="60"
          ><%= Joiner.on('\n').join(roomConfig.getAllowedParticipants()) %></textarea>
        <input type="submit" name="submit" value="submit"/>
    </form>
</body>
</html>
